﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moki;
using Moki.Interfaces;

namespace Moki.State
{
    public enum MockingState
    {
        Acting,
        RaisingEvent,
        Stubbing,
        Verifing,
    }

    /// <summary>
    /// Controlles mocking state. Regsters state updaters and notifiers.
    /// </summary>
    internal class StateController
    {
        private static readonly StateController _instance = new StateController();

        /// <summary>
        /// Current mocking state.
        /// </summary>
        private MockingState _currentState;

        /// <summary>
        /// Current mocking state event args.
        /// </summary>
        private object _currentStateArgs;

        public static StateController Instance
        {
            get { return _instance; }
        }
        
        /// <summary>
        /// Changes mocking state and state args.
        /// </summary>
        /// <param name="args"></param>
        public void StateChanged(object sender, StateEventArgs e)
        {
            _currentState = e.State;
            _currentStateArgs = e.Args;
        }

        /// <summary>
        /// Updates updater state by current state.
        /// </summary>
        /// <param name="updater"></param>
        public void UpdateState(IUpdateState updater)
        {
            updater.State = _currentState;
            updater.StateArgs = _currentStateArgs;
        }

        /// <summary>
        /// Register updater to get current state.
        /// </summary>
        /// <param name="updater"></param>
        public void RegisterUpdater(IUpdateState updater)
        {
            updater.UpdateState += this.UpdateState; 
        }

        /// <summary>
        /// Registers notifier. Adds permissions to change (update) state.
        /// </summary>
        /// <param name="notifier"></param>
        public void RegisterNotifier(StateChangedNotifier notifier)
        {
            notifier.StateChanged += this.StateChanged;
        }
    }
}
